package video;

import java.awt.Dimension;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.*;
import java.util.Vector;
import javax.swing.*;
import javax.media.*;
import javax.media.control.TrackControl;
import javax.media.datasink.DataSinkListener;
import javax.media.format.VideoFormat;
import javax.media.protocol.ContentDescriptor;
import javax.media.protocol.DataSource;

public class stream
{
	public static void main( String[] args){
	//our capture device

	CaptureDeviceInfo videoCDI = null;

	//our capture device's information

	CaptureDeviceInfo cdi;

	//all available capture devices on the system

	Vector devices;

	//all video capture devices on the system

	Vector<CaptureDeviceInfo> videoDevices;

	//all video formats available for our capture device

	Vector<Format> videoFormats;

	//our chosen video format. needs to be an array for when we

	//create the processor model. It takes an array so we can have

	//a video format and audio format, but we're only interested in

	//video. so its an array of 1.

	Format[] videoFormat = new Format[1];

	//the number of capture devices in the system

	int deviceCount = 0;
	

		//get the devices we can use

		devices = CaptureDeviceManager.getDeviceList ( null );

		//find out how many we have

		deviceCount = devices.size();

		//blah

		videoDevices =
			new Vector<CaptureDeviceInfo>();

		videoFormats =
			new Vector<Format>();

		//current formats for each device

		Format[] formats;

		//loop through the devices

		for ( int i = 0; i < deviceCount; i++ ) {

			//get the information for the current device

			cdi = (CaptureDeviceInfo) devices.elementAt ( i );

			//get the formats for the current device

			formats = cdi.getFormats();

			//loop through the formats for this device

			for ( int j=0; j<formats.length; j++ ) {

				//if its a video format we've got a "camera"

				if (formats[j] instanceof VideoFormat ) {

					//add it to our vector of video devices

					videoDevices.addElement(cdi);

					//add its format to our vector of formats

					videoFormats.addElement(formats[j]);

					//break;

				}

			}

		}

		//set the video device(should be the only one available on our server)

		cdi =(CaptureDeviceInfo) videoDevices.elementAt(0);

		System.
		out.print(cdi);

		//set up our format (might be more than one, but we'll just use the first)

		formats = cdi.getFormats();

		videoFormat[0] = (Format)videoFormats.elementAt(0);

		//set up the content descriptor

		ContentDescriptor CONTENT_DESCRIPTOR =

			new ContentDescriptor(ContentDescriptor.RAW_RTP);

		try {

			//create the processor model

			ProcessorModel pModel =
				new ProcessorModel(Manager.createDataSource(cdi.getLocator()),videoFormat,CONTENT_DESCRIPTOR);

			//set up the processor from the processor model

			Processor processor = Manager.createRealizedProcessor(pModel);

			//set up the data source <- this is where our video feed is coming from

			DataSource source = processor.getDataOutput();

			//set up the place to send the video to

			//******** needs to be the ip address of the server ****************

			//******** this string needs to be sent to the client ***************

			//******** so it knows where to look for the video feed **************

			//******** should probably use port 8080 to make sure it **************

			//******** works in arch for the presentation. **************

			MediaLocator dest =
				new MediaLocator("rtp://148.61.162.19:9060/video");

			//set up the datasink <- this actually pushes the video stream out

			//onto the network. we tell it where the video feed is coming from

			//and were it needs to be sent to as rtp packets

			DataSink dataSink =
				null;

			dataSink = Manager.createDataSink(source, dest);

			//open up the datasink

			System.
			out.println("Opening dataSink");

			dataSink.open();

			//start the datasink

			System.
			out.println("Starting dataSink");

			dataSink.start();

			//start the processor

			System.
			out.println("Starting processor");

			processor.start();

			//our video feed should now be streaming to the servers rtp://serverip:8080/video

			System.
			out.println("Everything Started");

			//When we're ready to stop feeding video we need to call the following

			/* dataSink.stop();

			 * dataSink.close();

			 * processor.stop();

			 * processor.close();

			 */

		}
		catch (Exception e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

		}
	}

}
